/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mobiubiq.directory.client.data;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.model.DataProvider;

/**
 *
 * @author Marnix Renne
 */
public class BookmarksDataProvider extends DataProvider {
    public static final String BOOKMARKS = "bookmarks";
    private RecordStore recordStore;
    
    public BookmarksDataProvider() {
        try {
            recordStore = RecordStore.openRecordStore(BOOKMARKS, true);
            RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
            while (re.hasNextElement()) {
                int id = re.nextRecordId();
                byte[] data = recordStore.getRecord(id);
                DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
                Bookmark bookmark = new Bookmark(id, dis);
                addItem(BOOKMARKS, bookmark);
            }
        } catch (RecordStoreNotOpenException ex) {
            Kuix.alert(ex);
        } catch (InvalidRecordIDException ex) {
            Kuix.alert(ex);
        } catch (RecordStoreException ex) {
            Kuix.alert(ex);
        } catch (IOException ex) {
            Kuix.alert(ex);
        }
    }
    
    public void close() {
        try {
            recordStore.closeRecordStore();
            recordStore = null;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
    
    public void addBookmark(Bookmark bookmark) throws RecordStoreNotOpenException, RecordStoreException {
        try {
            byte[] data = bookmark.toByteArray();
            int index = recordStore.addRecord(data, 0, data.length);
            bookmark.setRecordId(index);
            addItem(BOOKMARKS, bookmark);
        } catch (IOException ex) {
            throw new RecordStoreException(ex.getMessage());
        }
    }
    
    public void removeBookmark(Bookmark bookmark) throws RecordStoreNotOpenException, RecordStoreException {
        recordStore.deleteRecord(bookmark.getRecordId());
        this.removeItem(BOOKMARKS, bookmark);
    }
    
    public void updateBookmark(Bookmark bookmark) throws RecordStoreNotOpenException, RecordStoreException {
        try {
            byte[] data = bookmark.toByteArray();
            recordStore.setRecord(bookmark.getRecordId(), data, 0, data.length);
            this.dispatchUpdateEvent(BOOKMARKS);
        } catch (IOException ex) {
            throw new RecordStoreException(ex.getMessage());
        }
    }
    
    public void removeAll() {
        try {
            recordStore.closeRecordStore();
            RecordStore.deleteRecordStore(BOOKMARKS);
            RecordStore.openRecordStore(BOOKMARKS, true);
        } catch (RecordStoreNotOpenException ex) {
            Kuix.alert(ex);
        } catch (RecordStoreException ex) {
            Kuix.alert(ex);
        }
    }
}
